package pro.cherish.smartfile.config.swagger;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author 创建人：lys
 * @version 版本号：V1.0
 * <p>
 * ***************************修订记录************************************
 * 2020-5-28 lys 创建该类功能。
 * **********************************************************************
 * </p>
 * @ClassName 类名：JsonConfig
 * @Description 功能说明：重写Jackson objectMapper,处理json序列化的时间问题和通用处理配置
 * <p>

 * </p>
 * **********************************************************************
 * @date 创建日期： 2020-5-28 
 */
@Configuration
@Slf4j
public class JsonConfig {
	public static final String SYS_DATE_TIME_FORMART = "yyyy-MM-dd HH:mm:ss";

	@Bean
	@Primary
	public ObjectMapper mapper() {
		ObjectMapper mapper = new ObjectMapper();
		// 忽略json字符串中不识别的属性
		mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		// 忽略无法转换的对象(null)
		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
		// PrettyPrinter 格式化输出
		mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
		mapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
		// 属性是NULL不参与序列化
		// mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		// 日期类型字符串处理
		SimpleDateFormat dateFormat = new SimpleDateFormat(SYS_DATE_TIME_FORMART);
		log.info("Jackson时间格式化使用时区{}", TimeZone.getDefault());
		dateFormat.setTimeZone(TimeZone.getDefault());
		mapper.setDateFormat(dateFormat);
		JavaTimeModule timeModule = new JavaTimeModule();
		timeModule.addSerializer(Date.class, new DateSerializer(Boolean.FALSE, new SimpleDateFormat(SYS_DATE_TIME_FORMART)));
		timeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(SYS_DATE_TIME_FORMART)));
		timeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		timeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
		timeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(SYS_DATE_TIME_FORMART)));
		timeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
		timeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
		mapper.registerModule(timeModule).registerModule(new ParameterNamesModule()).registerModules(ObjectMapper.findModules());
		return mapper;
	}

}
